exports.index = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var browseTimes = 0;
    var redbag = 0;
    var redbagContent = 0;
    resultData.redPacket = [];
    //任务流创建
    async.waterfall([
        //查询机构浏览量
        //function(cb){

        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.searchOrganization+"/o_id/" + restfulParams.o_id,
        //             method : 'POST',
        //             data : {
        //                 fields:["o_id","o_browse_times"].join(",")
        //             }
        //         },
        //         function (err, data) {
        //             //判断是否有错误信息
        //             if (err) {
        //                 return cb(err, data);
        //             }

        //             browseTimes = data.data;

        //             cb();
        //         }
        //     );
        // },
        // //更新机构浏览量
        // function(cb){
        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.updateOrganization,
        //             method : 'POST',
        //             data : {
        //                 o_id:browseTimes[0].o_id,
        //                 o_browse_times:parseInt(browseTimes[0].o_browse_times)+1
        //             }
        //         },
        //         function (err, data) {
        //             //判断是否有错误信息
        //             if (err) {
        //                 return cb(err, data);
        //             }
        //             cb();
        //         }
        //     );
        // },
        //查询机构的banner图
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_organization_banner+"/ob_oid/" + restfulParams.o_id+"/sort_order/ob_order-asc",
                    method : 'POST',
                    data : {
                        fields:["ob_id", "ob_url", "ob_image_url","ob_title"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    // console.log(1111111);

                    resultData.organizationBannerData = data.data;

                    cb();
                }
            );
        },

        function(cb){
            R.SEND_HTTP(
                req,
                {
                    // url    : CORE_APIConfig.coreAPI.searchDiscountsActivity+"/oa_oid/" + restfulParams.o_id + "/oa_is_home/1/sort_order/dac_new_price-asc/sort_order/oa_create_time-desc/limit/1",
                    url    : CORE_APIConfig.coreAPI.searchDiscountsActivity+"/oa_oid/" + restfulParams.o_id + "/oa_is_home/1/sort_order/dac_new_price-asc/limit/1",
                    method : 'POST',
                    data : {
                        fields:["oa_id", "oa_title","oa_start_time","oa_end_time", "oa_join_count","oa_title_image","oa_type","dac_new_price as price","dac_old_price"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    // console.log(1111111);

                    resultData.organizationDiscountData = data.data;

                    cb();
                }
            );
        },

        //机构信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchOrganization+"/o_id/" + restfulParams.o_id,
                    method : 'POST',
                    data : {
                        fields:["o_id", "o_name", "o_logo","o_browse_times", "o_course_sales", "o_slogan", "o_desc", "o_address", "o_provinces", "o_city", "o_area", "o_create_time", "o_tags", "o_star_level","o_longitude","o_latitude","o_phone","is_cashback","cashback_money"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(222222);
                    resultData.organizationData = data.data;

                    cb();
                }
            );
        },

        //更新机构浏览量
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.updateOrganization,
                    method : 'POST',
                    data : {
                        o_id:resultData.organizationData[0].o_id,
                        o_browse_times:parseInt(resultData.organizationData[0].o_browse_times)+1
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(333333);
                    cb();
                }
            );
        },

        //约课信息
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_organization_course+"/oc_oid/"+restfulParams.o_id+"/oc_type/1/sort_order/oc_create_time-desc/skip/0/limit/1",
                    method : 'POST',
                    data : {
                        fields:["oc_id", "oc_title", "oc_title_image","oc_old_price", "oc_new_price", "oc_course_count", "oc_sales_volume"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(444444);
                    resultData.organizationAboutCourseData = data.data;

                    cb();
                }
            );
        },
        //精品课程
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_organization_course+"/oc_oid/"+restfulParams.o_id+"/oc_type/0/sort_order/oc_sales_volume-desc",
                    method : 'POST',
                    data : {
                        fields:["oc_id", "oc_title", "oc_title_image", "oc_new_price", "oc_age", "oc_count", "oc_course_count", "oc_sales_volume"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(555555);

                    resultData.organizationCourseData = data.data;

                    //判断是否需要返现
                    if(resultData.organizationData[0].is_cashback == 1){
                        
                        for(var i=0;i<resultData.organizationCourseData.length;i++){

                            if(parseFloat(resultData.organizationCourseData[i].oc_new_price) > parseFloat(resultData.organizationData[0].cashback_money)){
                                resultData.organizationCourseData[i].is_cashback = true;
                            }else{
                                resultData.organizationCourseData[i].is_cashback = false;
                            }

                        }

                        cb();

                    }else{
                        cb();
                    }

                    
                }
            );

        },
        //视频课程
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.organization_video_course+"/vc_oid/"+restfulParams.o_id,
                    method : 'POST'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(666666);
                    resultData.organizationVideoCourseData = data.data;

                    cb();
                }
            );

        },
        //机构活动
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchOrganizationActivity+"/oa_oid/"+restfulParams.o_id + "/sort_order/oa_order-oa_create_time-desc",
                    method : 'POST',
                    data : {
                        fields:["oa_id", "oa_title","oa_create_time", "oa_title_image", "oa_join_count", "oa_title_image","oa_title_image", "oa_type"].join(",")
                    }
                },
                function (err, data) {
                    
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(777777);
                    resultData.organizationActivityData = data.data;

                    cb();
                }
            );

        },

        function(cb){

        // //console.log(resultData.organizationActivityData);
        // console.log(8888888);
            //创建添加任务
            var tasks = [];

            // console.log(resultData.organizationActivityData)

            //循环节点列表
            resultData.organizationActivityData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {


                        //拼团
                        if(item.oa_type == 0){

                            R.SEND_HTTP(
                                req,
                                {
                                    url    : CORE_APIConfig.coreAPI.search_bulk_purchase_activity_config + "/bpac_oaid/" + item.oa_id,
                                    method : 'POST',
                                    data : {
                                        fields:["bpac_id","bpac_type","bpac_participation_price","bpac_open_price"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //console.log(data);
                                    if(data.data.length>0){

                                        if(data.data[0].bpac_type == 0 || data.data[0].bpac_type == 1){
                                            item.price =  data.data[0].bpac_open_price;
                                        }else{
                                            item.price =  data.data[0].bpac_participation_price;
                                        }

                                        if(item.price != ''){

                                            var priceArr = item.price.split("|");

                                            item.price = priceArr[priceArr.length - 1];

                                        }else{

                                            item.price = 0;

                                        }
                                        cb();

                                    }else{
                                        cb();
                                    }


                                }
                            );
                            //投票
                        }else if(item.oa_type == 1){
                            item.price =0;
                            cb();
                            //砍价价格
                        }else if(item.oa_type == 2){
                            R.SEND_HTTP(
                                req,
                                {
                                    url    : CORE_APIConfig.coreAPI.search_bargain_config + "/bc_oaid/" + item.oa_id,
                                    method : 'POST',
                                    data : {
                                        fields:["bc_most_price"].join(",")
                                    }
                                },
                                function (err, data) {

                                    // console.log(111,data);
                                    if(data.data.length>0){
                                        item.price =  data.data[0].bc_most_price;
                                    }
                                    cb();

                                }
                            );
                        }else if(item.oa_type == 3){
                            item.price =0;
                            cb();
                        }else if(item.oa_type == 4){
                            item.price =0;
                            cb();
                        }else if(item.oa_type == 5){

                            R.SEND_HTTP(
                                req,
                                {
                                    url    : CORE_APIConfig.coreAPI.search_discounts_activity_config + "/dac_oaid/" + item.oa_id,
                                    method : 'POST',
                                    data : {
                                        fields:["dac_id","dac_new_price"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //console.log(data);

                                    if(data.data.length>0){
                                        item.price =  data.data[0].dac_new_price;
                                    }


                                    cb();
                                }
                            );
                        }else if(item.oa_type == 6){
                            item.price =0;
                            cb()
                        }else{
                            cb();
                        }


                });

            });

            async.waterfall(tasks, cb);

        },
        //人气教师
        function(cb){

            
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_art_user_user_teacher_info+"/uti_oid/" + restfulParams.o_id, 
                    method : 'POST',
                    data : {
                        fields:["uti_id", "uti_name", "u_header_url"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(999999);
                    resultData.organizationTeacherData = data.data;

                    cb();
                }
            );

        },
        //机构相册
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_organization_photo+"/op_oid/" + restfulParams.o_id,
                    method : 'POST',
                    data : {
                        fields:["op_id", "op_fid", "op_image_url", "op_order"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationPhotoData = data.data;

                    cb();
                }
            );

        },
        //获取机构动态
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_question+"/fc_oid/" + restfulParams.o_id + "/skip/0/limit/1/sort_order/fc_create_time-desc",
                    method : 'POST',
                    data : {
                        fields:["fc_id", "fc_title", "fc_content","fc_type"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }
                    // console.log(1010101010);
                    resultData.organizationDyanmicData = data.data;

                    cb();
                }
            );


        },
        //机构评论回复带评论者与回复者信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_art_organization_comment_info+"/oc_oid/" + restfulParams.o_id+"/count/1",
                    method : 'POST'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.commentCount            = data.count;
                    resultData.organizationCommentData = data.data;
                    // console.log(12121212121);
                    cb();
                }
            );

        },
        //机构代金券信息
        // function(cb){

        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_oid/" + restfulParams.o_id,
        //             method : 'POST',
        //             data : {
        //                 fields:["cc_id", "cc_name", "cc_price", "cc_start_time", "cc_end_time"].join(",")
        //             }
        //         },
        //         function (err, data) {
        //             //判断是否有错误信息
        //             if (err) {
        //                 return cb(err, data);
        //             }

        //             resultData.organizationCouponCommentData = data.data;

        //             cb();
        //         }
        //     );

        // },
        //判断是否关注
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchOrganizationConcern + "/uoc_oid/" + restfulParams.o_id + "/uoc_uid/" + restfulParams.u_id,
                    method : 'POST'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    if(data.data.length > 0){
                        resultData.isConsern = true;
                    }else{
                        resultData.isConsern = false;
                    }
                    // console.log(1313131313);
                    cb();
                }
            );

        },
        // //判断用户是否领取艺魔方限时补贴
        // function(cb){

        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.searchCashCoupon+"/count/1/ucc_from_type/1/ucc_uid/" + restfulParams.u_id,
        //             method : 'GET'
        //         },
        //         function (err, data) {

        //             resultData.is_receive = data.count;

        //             cb();
        //         }
        //     );
        // },
        // //查询限时补贴代金券多少人已经领取
        // function(cb){

        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.searchCashCoupon+"/count/1/ucc_from_type/1",
        //             method : 'GET'
        //         },
        //         function (err, data) {

        //             resultData.receiveCount = data.count;

        //             cb();
        //         }
        //     );
        // },
        //查询限时补贴代金券结束时间
        // function(cb){

        //     R.SEND_HTTP(
        //         req,
        //         {
        //             url    : CORE_APIConfig.coreAPI.search_platform_coupon +"/pc_type/1",
        //             method : 'POST',
        //             data : {
        //                 fields:["pc_end_time"].join(",")
        //             }
        //         },
        //         function (err, data) {

        //             resultData.endTime = data.data ;

        //             cb();
        //         }
        //     );
        // },
        //查询红包活动
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchOrganizationActivity+"/oa_oid/"+restfulParams.o_id + "/oa_type/6",
                    method : 'POST',
                    data : {
                        fields:["oa_id", "oa_title", "oa_title_image","oa_end_time", "oa_join_count", "oa_title_image","oa_title_image", "oa_type"].join(",")
                    }
                },
                function (err, data) {
                    // console.log(data);
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }



                    // console.log("-----------");
                    // console.log(data.data);
                    // console.log("-----------");
                    redbag = data.data;

                    if(redbag.length<=0){
                        redbag = [];
                        return cb();
                    }

                    if(common.nowTime(false) > redbag[0].oa_end_time){
                        redbag = [];
                    }

                    cb();
                }
            );


        },
        //查询红包活动内容
        function(cb){
            if(redbag.length>0){
                R.SEND_HTTP(
                    req,
                    {
                        url    : CORE_APIConfig.coreAPI.search_redbag_cash_coupon+"/rba_oaid/"+redbag[0].oa_id,
                        method : 'POST',
                        data : {
                            fields:["cc_id","cc_name","cc_price","cc_start_time","cc_end_time","cc_type","cc_fid","cc_max_price","cc_desc"].join(",")
                        }
                    },
                    function (err, data) {
                        // console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }


                        resultData.redPacket = data.data;
                        // console.log("=========");
                        // console.log( "red ："+ resultData.redPacket);
                        // console.log("=========");






                        cb();
                    }
                );
            }else{
                cb();
            }
        },

        /************************************************/
        function (cb) {
            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.redPacket.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //查询对该用户的回复
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchUserCashCoupon + "/ucc_oid/"+restfulParams.o_id+"/ucc_uid/" + restfulParams.u_id + "/ucc_ccid/" + item.cc_id,
                            method: 'POST',
                            data: {
                                fields: ["ucc_id"].join(",")
                            }
                        },
                        function (err, data) {
                            var is_exist = data.data;
                            // console.log("*******")
                            // console.log(is_exist);
                            console.log("*******")
                            if (err) {
                                return cb(err, data);
                            }

                            if (is_exist.length <= 0) {
                                item.is_get = 0;
                            } else {
                                item.is_get = 1;
                            }
                            cb();
                        });
                });
            });
            async.waterfall(tasks, cb);
        }


        ],function(err, result){

            //红包内容
            // if(redbag.length == 0){
            //     resultData.redPacket = [];
            // }else{
            //     resultData.redPacket = redbag[0];
            // }
            //返回最终拼接结果
            RES.response(res,true,resultData);

        }
    );

};